package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.tradewar.*;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserTradeWarBuildingDaoImpl extends TempDataObjectDao<UserTradeWarBuilding> {

	public Map<Integer, UserTradeWarBuilding> getUserTradeWarBuildingMap(long userId) {
		String sql = "select * from t_u_trade_war_building where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, UserTradeWarBuilding> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarBuilding data = parse(rs);
				dataMap.put(data.getType(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserTradeWarBuilding data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_trade_war_building(userId,type,level,lastGetTime,lastValue,lastValueChangeTime,lastOutputSpeed,totalGetRewardTimes) VALUES(?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getType());
			call.setInt(i++, data.getLevel());
			call.setLong(i++, data.getLastGetTime());
			call.setString(i++, data.getLastValue().toString());
			call.setLong(i++, data.getLastValueChangeTime());
			call.setString(i++, data.getLastOutputSpeed().toString());
			call.setLong(i++, data.getTotalGetRewardTimes());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTradeWarBuilding data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_trade_war_building SET level=?,lastGetTime=?,lastValue=?,lastValueChangeTime=?,lastOutputSpeed=?,totalGetRewardTimes=? WHERE userId=? and type=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getLevel());
			call.setLong(i++, data.getLastGetTime());
			call.setString(i++, data.getLastValue().toString());
			call.setLong(i++, data.getLastValueChangeTime());
			call.setString(i++, data.getLastOutputSpeed().toString());
			call.setLong(i++, data.getTotalGetRewardTimes());
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getType());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTradeWarBuilding parseData(ResultSet rs) throws SQLException {
		UserTradeWarBuilding data = new UserTradeWarBuilding();
		data.setUserId(rs.getLong("userId"));
		data.setType(rs.getInt("type"));
		data.setLevel(rs.getInt("level"));
		data.setLastGetTime(rs.getLong("lastGetTime"));
		String value = rs.getString("lastValue");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setLastValue(new BigInteger(value));
		}

		value = rs.getString("lastOutputSpeed");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setLastOutputSpeed(new BigInteger(value));
		}
		data.setLastValueChangeTime(rs.getLong("lastValueChangeTime"));
		data.setTotalGetRewardTimes(rs.getLong("totalGetRewardTimes"));
		return data;
	}
}
